/*
* IN.java
* Copyright (C) 2003
* 
* $Id: IN.java,v 1.7 2004/12/16 22:45:55 hzi Exp $
*/
/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
using System;
using Globals = Suake2.UI.Globals;
using CL_input = Suake2.UI.client.CL_input;
using Key = Suake2.UI.client.Key;
using Cmd = Suake2.UI.game.Cmd;
using usercmd_t = Suake2.UI.game.usercmd_t;
using Cvar = Suake2.UI.qcommon.Cvar;
using xcommand_t = Suake2.UI.qcommon.xcommand_t;
using Math3D = Suake2.UI.util.Math3D;
namespace Suake2.UI.sys
{
	
	/// <summary> IN</summary>
	public sealed class IN:Globals
	{
		private class AnonymousClassxcommand_t:xcommand_t
		{
			public override void  execute()
			{
				jake2.sys.IN.MLookDown();
			}
		}
		private class AnonymousClassxcommand_t1:xcommand_t
		{
			public override void  execute()
			{
				jake2.sys.IN.MLookUp();
			}
		}
		private class AnonymousClassxcommand_t2:xcommand_t
		{
			public override void  execute()
			{
				jake2.sys.IN.Force_CenterView_f();
			}
		}
		private class AnonymousClassxcommand_t3:xcommand_t
		{
			public override void  execute()
			{
				jake2.sys.IN.toggleMouse();
			}
		}
		
		internal static bool mouse_avail = true;
		
		internal static bool mouse_active = false;
		
		internal static bool ignorefirst = false;
		
		internal static int mouse_buttonstate;
		
		internal static int mouse_oldbuttonstate;
		
		internal static int old_mouse_x;
		
		internal static int old_mouse_y;
		
		internal static bool mlooking;
		
		public static void  ActivateMouse()
		{
			if (!mouse_avail)
				return ;
			if (!mouse_active)
			{
				KBD.mx = KBD.my = 0; // don't spazz
				install_grabs();
				mouse_active = true;
			}
		}
		
		public static void  DeactivateMouse()
		{
			// if (!mouse_avail || c == null) return;
			if (mouse_active)
			{
				uninstall_grabs();
				mouse_active = false;
			}
		}
		
		private static void  install_grabs()
		{
			Globals.re.KeyboardHandler.installGrabs();
			ignorefirst = true;
		}
		
		private static void  uninstall_grabs()
		{
			Globals.re.KeyboardHandler.uninstallGrabs();
		}
		
		public static void  toggleMouse()
		{
			if (mouse_avail)
			{
				mouse_avail = false;
				DeactivateMouse();
			}
			else
			{
				mouse_avail = true;
				ActivateMouse();
			}
		}
		
		public static void  Init()
		{
			in_mouse = Cvar.Get("in_mouse", "1", CVAR_ARCHIVE);
			in_joystick = Cvar.Get("in_joystick", "0", CVAR_ARCHIVE);
		}
		
		public static void  Shutdown()
		{
			mouse_avail = false;
		}
		
		public static void  Real_IN_Init()
		{
			// mouse variables
			Globals.m_filter = Cvar.Get("m_filter", "0", 0);
			Globals.in_mouse = Cvar.Get("in_mouse", "1", CVAR_ARCHIVE);
			Globals.freelook = Cvar.Get("freelook", "1", 0);
			Globals.lookstrafe = Cvar.Get("lookstrafe", "0", 0);
			Globals.sensitivity = Cvar.Get("sensitivity", "3", 0);
			Globals.m_pitch = Cvar.Get("m_pitch", "0.022", 0);
			Globals.m_yaw = Cvar.Get("m_yaw", "0.022", 0);
			Globals.m_forward = Cvar.Get("m_forward", "1", 0);
			Globals.m_side = Cvar.Get("m_side", "0.8", 0);
			
			Cmd.AddCommand("+mlook", new AnonymousClassxcommand_t());
			Cmd.AddCommand("-mlook", new AnonymousClassxcommand_t1());
			
			Cmd.AddCommand("force_centerview", new AnonymousClassxcommand_t2());
			
			Cmd.AddCommand("togglemouse", new AnonymousClassxcommand_t3());
			
			IN.mouse_avail = true;
		}
		
		public static void  Commands()
		{
			int i;
			
			if (!IN.mouse_avail)
				return ;
			
			KBD kbd = Globals.re.KeyboardHandler;
			for (i = 0; i < 3; i++)
			{
				if ((IN.mouse_buttonstate & (1 << i)) != 0 && (IN.mouse_oldbuttonstate & (1 << i)) == 0)
					kbd.Do_Key_Event(Key.K_MOUSE1 + i, true);
				
				if ((IN.mouse_buttonstate & (1 << i)) == 0 && (IN.mouse_oldbuttonstate & (1 << i)) != 0)
					kbd.Do_Key_Event(Key.K_MOUSE1 + i, false);
			}
			IN.mouse_oldbuttonstate = IN.mouse_buttonstate;
		}
		
		public static void  Frame()
		{
			
			if (!cl.refresh_prepped || cls.key_dest == key_console || cls.key_dest == key_menu)
				DeactivateMouse();
			else
				ActivateMouse();
		}
		
		public static void  CenterView()
		{
			cl.viewangles[PITCH] = - Math3D.SHORT2ANGLE(cl.frame.playerstate.pmove.delta_angles[PITCH]);
		}
		
		public static void  Move(usercmd_t cmd)
		{
			if (!IN.mouse_avail)
				return ;
			
			if (Globals.m_filter.value_Renamed != 0.0f)
			{
				KBD.mx = (KBD.mx + IN.old_mouse_x) / 2;
				KBD.my = (KBD.my + IN.old_mouse_y) / 2;
			}
			
			IN.old_mouse_x = KBD.mx;
			IN.old_mouse_y = KBD.my;
			
			//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
			KBD.mx = (int) (KBD.mx * Globals.sensitivity.value_Renamed);
			//UPGRADE_WARNING: Data types in Visual C# might be different.  Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'"
			KBD.my = (int) (KBD.my * Globals.sensitivity.value_Renamed);
			
			// add mouse X/Y movement to cmd
			if ((CL_input.in_strafe.state & 1) != 0 || ((Globals.lookstrafe.value_Renamed != 0) && IN.mlooking))
			{
				cmd.sidemove = (short) (cmd.sidemove + Globals.m_side.value_Renamed * KBD.mx);
			}
			else
			{
				Globals.cl.viewangles[YAW] -= Globals.m_yaw.value_Renamed * KBD.mx;
			}
			
			if ((IN.mlooking || Globals.freelook.value_Renamed != 0.0f) && (CL_input.in_strafe.state & 1) == 0)
			{
				Globals.cl.viewangles[PITCH] += Globals.m_pitch.value_Renamed * KBD.my;
			}
			else
			{
				cmd.forwardmove = (short) (cmd.forwardmove - Globals.m_forward.value_Renamed * KBD.my);
			}
			KBD.mx = KBD.my = 0;
		}
		
		internal static void  MLookDown()
		{
			mlooking = true;
		}
		
		internal static void  MLookUp()
		{
			mlooking = false;
			CenterView();
		}
		
		internal static void  Force_CenterView_f()
		{
			Globals.cl.viewangles[PITCH] = 0;
		}
	}
}